import React, { useState, useEffect } from 'react'; //引入
import List from '../components/admins/List';
import Add from '../components/admins/Add';
import Set from '../components/admins/Set';
import Searche from '../components/admins/Search';
// import { userAll } from "../services/admin"
import { useSelector, useDispatch } from "react-redux";
import { setAdmin } from '../store/slice/adminSlice';
import { userAll } from "../services/admin"

export default function Admin() {
    let dispatch = useDispatch();

    useEffect(() => {
        show()
    }, [1])



    const [isModalVisible, setIsModalVisible] = useState(false);
    // let [students, setStudents] = useState([]); //学生列表状态
    // let [student, setStudent] = useState('');//单个学生状态
    let [setId, setSetId] = useState('');//修改的状态
    let [search, setSearch] = useState("");//搜索的在状态
    let [pagination, setPagination] = useState({
        defaultCurrent: 1,
        showSizeChanger: true,
        showQuickJumper: true,
        showTotal: (total) => `总共 ${total} 条`,
        pageSizeOptions: [5, 10, 15, 20],
    });



    let show = async (obj) => { //判断是否传了类型和值,如果有的话就赋值给搜索状态，
        //没有的话就就将上一次的状态赋值给它
        if (obj) {
            obj = { ...search, ...obj };
            setSearch(obj)
        } else {
            obj = search
        }
        let data = await userAll(obj);
        console.log("搜索数据",data.data);
        dispatch(setAdmin(data.data))
        setPagination({
            ...pagination,
            current: parseInt(data.data.current),
            pageSize: data.data.pageSize,
            total: data.data.total
        });
    };

    return (
        <div>
            <div style={{ display: 'flex' }} >
                <Add show={show} ></Add>
                <Searche show={show} ></Searche>
            </div>
            <List pagination={pagination} show={show}></List>
            <Set
                setIsModalVisible={setIsModalVisible}
                isModalVisible={isModalVisible}
                show={show}
                setId={setId}></Set>
        </div>
    )
}
